﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_bug_ChangeStatusofBug : System.Web.UI.Page
{
    int bugid = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack && ddlProjectName.Text != null)
        //ltrchangeStatus.Text = "";
        //  CbxChangeStatus.Checked = false;

        ltrchangeStatus.Text = "hide";
        if (!IsPostBack)
        {
            if (ddlProjectName.Text != null)
            {
                ltrchangeStatus.Text = "hide";
                ltrbtn.Text = "hide";
                ltrDisplay.Text = "hide";

                grDisplay.SelectedIndex = -1;
            }
            else
            {
                ltrchangeStatus.Text = "";
                ltrbtn.Text = "";
                ltrDisplay.Text = "";
            }

            var projects = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects.Where(i => i.Username == Master.Master.CurrentUserManagement.CurrentUser.Username)
                           select item.Project;

            foreach (Project item in projects)
            {
                ListItem projectItem = new ListItem(item.Name, item.ProjectId.ToString());
                ddlProjectName.Items.Add(projectItem);
            }

            // adding enum type of status of bug for change status

            foreach (var item in EnumUtility.EnumToList<BugManagement.Status>())
            {
                ddlStatus.Items.Add(EnumUtility.GetEnumNiceName(item));
            }
        }
    }

    protected void ddlProjectName_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idproject = Convert.ToInt32(ddlProjectName.SelectedItem.Value);
        RefreshGView();
    }

    public void RefreshGView()
    {
        int idproject = Convert.ToInt32(ddlProjectName.SelectedItem.Value);
        var bugs = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Bugs.Where(i => i.ProjectId == idproject)
                   select new
                   {
                       item.BugId,
                       item.Summary,
                       item.Status,
                       item.ReportedUser,
                       item.ReportedDate
                   };
        grDisplay.DataSource = bugs;
        grDisplay.DataBind();
        ltrDisplay.Text = "";
    }

    protected void grDisplay_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = grDisplay.SelectedRow;
        bugid = Convert.ToInt32(row.Cells[1].Text);
        int databaseStatus = Convert.ToInt32(row.Cells[3].Text);
        ddlStatus.SelectedIndex = databaseStatus;
        Response.Write(bugid);
        ltrbtn.Text = "";
        ltrchangeStatus.Text = "";
        Session["bugid"] = bugid;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        BugManagement objBugManagement = new BugManagement(Master.Master.CurrentUserManagement.CurrentUser);
        bugid = Convert.ToInt32(Session["bugid"]);
        bool result = objBugManagement.SetBugStatus(bugid, ddlStatus.SelectedIndex);

        RefreshGView();

        if (result == true)
        {
            ltrchangeStatus.Text = "Save status successfully !!!";
        }
        else
        {
            ltrchangeStatus.Text = "Fail !!!";
        }
    }
}